package io.github.wslxm.springbootplus2.manage.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.wslxm.springbootplus2.core.base.service.impl.BaseServiceImpl;
import io.github.wslxm.springbootplus2.core.result.ResultType;
import io.github.wslxm.springbootplus2.core.utils.XjBeanUtil;
import io.github.wslxm.springbootplus2.core.utils.tree.XjTreeUtil;
import io.github.wslxm.springbootplus2.core.utils.validated.XjValidUtil;
import io.github.wslxm.springbootplus2.manage.sys.mapper.SysDepMapper;
import io.github.wslxm.springbootplus2.manage.sys.model.dto.SysDepDTO;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysDep;
import io.github.wslxm.springbootplus2.manage.sys.model.query.SysDepQuery;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysDepVO;
import io.github.wslxm.springbootplus2.manage.sys.service.SysDepService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * base--sys--组织机构 ServiceImpl
 *
 * <p>
 * ::本代码由[兮家小二]提供的代码生成器生成,如有问题,请手动修改 ::作者CSDN:https://blog.csdn.net/qq_41463655
 * </p>
 *
 * @author ws
 * @email 1720696548@qq.com
 * @date 2022-12-29 09:57:30
 */
@Service
public class SysDepServiceImpl extends BaseServiceImpl<SysDepMapper, SysDep> implements SysDepService {


    @Override
    public List<SysDepVO> tree(SysDepQuery query) {
        LambdaQueryWrapper<SysDep> queryWrapper = new LambdaQueryWrapper<SysDep>()
                .orderByAsc(SysDep::getSort)
                .orderByDesc(SysDep::getCreateTime);
        List<SysDep> list = this.list(queryWrapper);
        List<SysDepVO> listVo = XjBeanUtil.listVo(list, SysDepVO.class);
        return XjTreeUtil.nextTree(listVo, "0").getChildren();
    }


    @Override
    public BasePage<SysDepVO> findPage(SysDepQuery query) {
        LambdaQueryWrapper<SysDep> queryWrapper = new LambdaQueryWrapper<SysDep>()
                .orderByAsc(SysDep::getSort)
                .orderByDesc(SysDep::getCreateTime);
        queryWrapper.likeRight(StringUtils.isNotBlank(query.getPid()), SysDep::getPid, query.getPid());
        queryWrapper.likeRight(StringUtils.isNotBlank(query.getName()), SysDep::getName, query.getName());
        queryWrapper.likeRight(StringUtils.isNotBlank(query.getCode()), SysDep::getCode, query.getCode());
        queryWrapper.eq(query.getDisable() != null, SysDep::getDisable, query.getDisable());
        Page<SysDep> page = this.page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);
        return XjBeanUtil.pageVo(page, SysDepVO.class);
    }

    @Override
    public SysDepVO findId(String id) {
        return XjBeanUtil.convert(this.getById(id), SysDepVO.class);
    }

    @Override
    public String insert(SysDepDTO dto) {
        this.verifyCodeRepeat(dto.getCode(), null);
        SysDep entity = dto.convert(SysDep.class);
        boolean b = this.save(entity);
        return entity.getId();
    }

    @Override
    public boolean upd(String id, SysDepDTO dto) {
        this.verifyCodeRepeat(dto.getCode(), id);
        SysDep entity = dto.convert(SysDep.class);
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public boolean del(String id) {
        List<SysDep> list = this.list(new LambdaQueryWrapper<SysDep>().select(SysDep::getId, SysDep::getPid));
        List<SysDepVO> listVos = XjBeanUtil.listVo(list, SysDepVO.class);
        List<String> nextIds = XjTreeUtil.getNextIds(listVos, id);
        nextIds.add(id);
        return this.removeBatchByIds(nextIds);
    }


    /**
     * 部门code重复验证
     *
     * @author wangsong
     * @mail 1720696548@qq.com
     * @date 2022/8/20 0020 14:33 验证
     * @version 1.0.0
     */
    private void verifyCodeRepeat(String code, String excludeId) {
        long count = this.count(new LambdaQueryWrapper<SysDep>()
                .eq(SysDep::getCode, code)
                .ne(excludeId != null, SysDep::getId, excludeId)
        );
        XjValidUtil.isTrue(count > 0, ResultType.BASE_CODE_REPEAT);
    }

}
